﻿using System;
using UnityEngine;
using System.Collections.Generic;
using Manager;

public class SkillActionController
{
	public SkillEffectController effect;

	public SkillActionVo vo;

	private List<GameObject> skillEffectList;

	private bool running = false;

	public void Init(SkillActionVo _vo){
		vo = _vo;
		if (skillEffectList == null)
			skillEffectList = new List<GameObject> ();
		running = false;
	}

	public void Update(){
		if (!running && effect.rootTime >= vo.delayTime) {
			Debug.LogError ("time:" + effect.rootTime + "内容 ：" + vo.ToString());
			Execute ();
		}
	}

	public void Execute(){
		switch (vo.type) {
			case SkillActionEnum.playSkillAnimation:
			effect.caster.GetComponent<AnimationControl> ().PlayAnimation (vo.animation);
				break;

			case SkillActionEnum.playGetHitAnimation:
				if (effect.targets != null) {
					foreach (GameObject obj in effect.targets) {
					obj.GetComponent<AnimationControl> ().PlayAnimation (vo.animation);
					}
				}
				break;
		case SkillActionEnum.playSkillEffect:
            ShowEffect(effect.caster);
			break;
		case SkillActionEnum.playGetHitEffect:
			foreach (GameObject target in effect.targets) {
                ShowEffect(target);
			}
			break;
		}
		running = true;
	}

	private void ShowEffect(GameObject target)
    {
        string path = AppConst.GetSkillEffectAssetBundlePath(vo.resourceName);
        GameManager.instance.resourceManager.LoadAssetBundle(
           path,
            null,
            () => {
                AssetBundle ab = GameManager.instance.resourceManager.GetAssetBundle(path);
                object effect = ab.LoadAsset(vo.resourceName.ToLower());
                GameObject effectObj = GameObject.Instantiate(effect as GameObject);
                effectObj.transform.SetParent(target.transform);
                effectObj.transform.localPosition = Vector3.zero;
                effectObj.transform.localScale = Vector3.one;
                effectObj.SetActive(true);
            }
            );
    }

	public void Dispose(){
		if (skillEffectList != null) {
			foreach (GameObject obj in skillEffectList) {
				GameObject.Destroy (obj);
			}
		}
		running = false;
	}


}

